define(function (require) {
    var Base = require('group/common/main');
    var utils = require('common/utils/home');
    var Mask = require('common/mask');
    var mask = new Mask();
    require('J_widget');
    var waterfall = require('common/waterfall');
    var ShopFavsTpl = require('text!./templates/shop_favs.html');
    var ItemFavsTpl = require('text!./templates/item_favs.html');
    var ItemAttrModal = require('./item_attr_modal');
    var ItemDetailAttrTpl = require('text!./templates/item_detail_attr.html');
    var ItemCommentTpl = require('text!./templates/item_comment.html');
    var WXHelper = require('common/WXHelper');

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(this, [params]);
            if (_this['page_' + _global.a]) {
                _this['page_' + _global.a]();
            }
        },
        page_index: function () {
            var _this = this;
            if (_global.is_weixin_browser) {
                WXHelper.share({
                    title: _global.item.title,
                    desc: _global.item.title,
                    link: _global.item.url,
                    imgUrl: _global.item.img
                });
            }
            _this.$('#J_goods_img .cut-img').css({
                width: _this.page_width() + 'px',
                height: _this.page_width() + 'px'
            });
            new Swiper('.swiper-container', {
                pagination: '.swiper-pagination'
            });

            new Swiper('.item-rec', {
                slidesPerView: 3.4,
                onTouchEnd: function () {
                    $('.item-rec img').trigger('scroll');
                },
                onInit: function () {
                    _this.$('.swiper-slide .img img').each(function () {
                        $(this).css({
                            'width': $(this).parent().width() + 'px',
                            'height': $(this).parent().width() + 'px'
                        });
                    });
                }
            });

            var itemAttrModal = new ItemAttrModal();

            /*_this.$('#J_member_contact').click(function () {
                var $this = $(this);
                layer.open({
                    type: 2,
                    title: '商家客服',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['100%', '100%'],
                    content: _.U({m: 'member', a: 'contact', mid: $this.data('mid'), iframe: '1'})
                });
            });*/

            var render_shop_favs = function () {
                $('.J_shop_favs').html(_this.render(ShopFavsTpl, _global.item));
            };
            var render_item_favs = function () {
                $('.J_item_favs').html(_this.render(ItemFavsTpl, _global.item));
            };

            render_shop_favs();
            render_item_favs();

            _this.$('.J_item_favs').click(function () {
                var $this = $(this);
                utils.ajaxPost({a: 'favs', id: $(this).data('id')}, function (resp) {
                    if ($('i', $this).hasClass('collect')) {
                        _global.item.is_favs = 1;
                    }
                    else {
                        _global.item.is_favs = 0;
                    }
                    render_item_favs();
                    mask.success('操作成功');
                });
            });
            _this.$('.js-show-item-attr').click(function () {
                var $this = $(this);
                var url = 'order_max';
                 utils.ajaxGet({a: url, id: $(this).data('id')}, function (resp) {
                     
                     if(resp.status==1){
                           itemAttrModal.show();
                     }else{
                          alert(resp.msg);
                     }
                      
                    });
            });
            //加载商品属性
            var isLoadAttr = false;
            var comment_waterfall = new waterfall({
                container: '#J_waterfall_comment',
                loading_bar: '#J_wall_loading_comment',
                spage: 1,
                auto_load: false,
                ajax_url: _.U({a: 'ajax_item_comment_lists', item_id: _global.item.id}),
                content_html: function (resp) {
                    return _this.render(ItemCommentTpl, resp.data);
                },
                fn: function () {
                    _this.setupItemWaterfall();
                }
            });
            var hot_item_waterfall = new waterfall({
                container: '#J_wall_hot_item',
                loading_bar: '#J_wall_loading_hot_item',
                spage: 1,
                auto_load: false,
                ajax_url: _.U({a: 'ajax_hot_item_lists', item_id: _global.item.id}),
                fn: function () {
                    _this.setupItemWaterfall();
                }
            });

            var title_sel = '.item-detail-title';
            var detail_title_width = $(title_sel).width();
            var detail_title_left = $(title_sel).offset().left;

            function bind_detail_title() {
                if ($('.item-detail').offset().top <= $(window).scrollTop()) {
                    $(title_sel).css({
                        position: 'fixed',
                        top: '0px',
                        left: detail_title_left + 8 + 'px',
                        width: detail_title_width + 'px',
                        'z-index': window.Z_INDEX
                    });
                }
                else {
                    $(title_sel).css({
                        position: 'static'
                    });
                }
            }

            bind_detail_title();
            $(window).bind('scroll', function () {
                bind_detail_title();
            });
            $('.item-detail .col-xs-12').css({
                'min-height': $(window).height() + 'px'
            });

            _this.$('.item-detail-title .item').click(function () {
                var type = $(this).data('type');
                if (type == 'item_attr') {
                    if (isLoadAttr) {
                        return;
                    }
                    utils.ajaxGet({a: 'ajax_item_attr', id: _global.item.id}, function (resp) {
                        _this.$('#J_item_attr').html(_this.render(ItemDetailAttrTpl, resp.data));
                        isLoadAttr = true;
                    });
                }
                if (type == 'comment') {
                    comment_waterfall.start({
                        spage: 1
                    });
                }
                if (type == 'hot_item') {
                    hot_item_waterfall.start({
                        spage: 1
                    });
                }
                comment_waterfall.settings.auto_load = type == 'comment';
                hot_item_waterfall.settings.auto_load = type == 'hot_item';
                $(window).scrollTop($('.item-detail').offset().top - $(title_sel).height());
            });
            _this.$('#J_item_comment_title a').click(function () {
                $(this).addClass('ac').siblings().removeClass('ac');
            });
            _this.$('#J_item_comment_all').click(function () {
                comment_waterfall.start({
                    spage: 1,
                    ajax_params: {type: 'all'}
                });
            });
            _this.$('#J_item_comment_img').click(function () {
                comment_waterfall.start({
                    spage: 1,
                    ajax_params: {type: 'img'}
                });
            });
        },
        page_lists: function () {
            var _this = this;
            _this.waterfall();
        },
        page_buy_list: function () {
            var _this = this;
            var BuyListTpl = require('text!./templates/buy_list.html');

            new waterfall({
                auto_load: false,
                content_html: function (resp) {
                    return _this.render(BuyListTpl, resp.data);
                }
            }).start({
                spage: 1
            });
        },
        page_screen: function () {
            $('.js-change-price').on('click', function () {
                var sprice = $(this).attr('data-sprice');
                var eprice = $(this).attr('data-eprice');
                $('input[name="sprice"]').val(sprice);
                $('input[name="eprice"]').val(eprice);
                $(this).find('span').addClass('ac');
                $(this).siblings().find('span').removeClass('ac');
                if (!sprice && !eprice) {
                    $('.js-change-price').find('span').removeClass('ac');
                    $('.js-change-price.all').find('span').addClass('ac');
                }
            });
        }
    });
});